package org.yhtch.jswtdic;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.yhtch.jswtdic.res.R;
//import org.yhtch.util.HtmlEntityComplexChar;

public class DicUI {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final int iWindowWidth = 640;
		final int iWindowHeight = 480;

		final DicCore dicCore = new DicCore();

		final Display display = new Display();
		final Shell shell = new Shell(display, SWT.MIN | SWT.CLOSE | SWT.TITLE
				| SWT.BORDER);

		shell.setText(R.getR().getStrRes("app_name"));
		shell.setImage(R.getR().getLogoImage(display));
		shell.setLayout(new FillLayout());
		shell.addShellListener(new ShellAdapter() {
			public void shellIconified(ShellEvent e) {
				// System.out.println("ok");
				shell.setVisible(false);
			}

			public void shellClosed(ShellEvent e) {
				// super.shellActivated(e);
				dicCore.close();
			}
		});

		final Tray tray = display.getSystemTray();
		if (tray == null) {
			// System.out.println ("The system tray is not available");
		} else {
			final TrayItem item = new TrayItem(tray, SWT.NONE);
			item.setToolTipText(R.getR().getStrRes("tary_icon_tooltip"));
			item.addListener(SWT.DefaultSelection, new Listener() {
				public void handleEvent(Event event) {
					if (shell.getVisible()) {
						shell.setVisible(false);
					} else {
						shell.setVisible(true);
						shell.setMinimized(false);
						shell.forceActive();
					}
				}
			});
			item.setImage(R.getR().getLogoImage(display));
			final Menu trayMenu = new Menu(shell, SWT.POP_UP);
			final MenuItem trayMenuItem0 = new MenuItem(trayMenu, SWT.CHECK);
			trayMenuItem0.setText(R.getR().getStrRes("search_net"));
			trayMenuItem0.setImage(R.getR().getNetImage(display));
			trayMenuItem0.setSelection(dicCore.isFindFromNet());
			trayMenuItem0.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					// trayMenuItem0.setSelection(!trayMenuItem0.getSelection());
					dicCore.setFindFromNet(trayMenuItem0.getSelection());
				}
			});
			final MenuItem trayMenuItem1 = new MenuItem(trayMenu, SWT.PUSH);
			trayMenuItem1.setText(R.getR().getStrRes("show_hide"));
			trayMenuItem1.setImage(R.getR().getShowImage(display));
			trayMenuItem1.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					if (shell.getVisible()) {
						shell.setVisible(false);
					} else {
						shell.setVisible(true);
						shell.setMinimized(false);
						shell.forceActive();
					}
				}
			});
			final MenuItem trayMenuItem2 = new MenuItem(trayMenu, SWT.PUSH);
			trayMenuItem2.setText(R.getR().getStrRes("app_exit"));
			trayMenuItem2.setImage(R.getR().getExitImage(display));
			trayMenuItem2.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					shell.close();
					display.dispose();
				}
			});
			item.addListener(SWT.MenuDetect, new Listener() {
				public void handleEvent(Event event) {
					trayMenu.setVisible(true);
				}
			});
		}

		final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);

		TabItem searchItem = new TabItem(tabFolder, SWT.NONE);
		searchItem.setText(R.getR().getStrRes("search_word"));
		Composite searchComposite = new Composite(tabFolder, SWT.NONE);
		GridLayout layout0 = new GridLayout();
		layout0.numColumns = 1;
		// layout0.pack=false;
		// layout0.justify=true;
		// layout0.wrap=true;
		// layout0.justify=true;
		searchComposite.setLayout(layout0);
		// System.out.println(searchComposite.getClientArea().width);
		final Text textSearchWord = new Text(searchComposite, SWT.BORDER|SWT.SINGLE);
		GridData gd0 = new GridData();
		gd0.horizontalAlignment = GridData.FILL;
		gd0.grabExcessHorizontalSpace = true;
		textSearchWord.setLayoutData(gd0);
		textSearchWord.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
		final Text textSearchWordExplain = new Text(searchComposite, SWT.READ_ONLY
				| SWT.BORDER | SWT.WRAP);
		// final Label explainShow = new Label(searchComposite,
		// SWT.BORDER|SWT.SINGLE|SWT.WRAP);
		GridData gd1 = new GridData();
		gd1.horizontalAlignment = GridData.FILL;
		gd1.grabExcessHorizontalSpace = true;
		gd1.verticalAlignment = GridData.FILL;
		gd1.grabExcessVerticalSpace = true;
		textSearchWordExplain.setLayoutData(gd1);
		// explainShow.setText(HtmlEntityComplexChar.decode("[ 'pikt&#643;&#601;
		// ]"));
		// explainShow.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		textSearchWord.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent event) {
				// text1.setTopIndex (text0.getTopIndex ());
				// text1.setSelection (event.start, event.end);
				String word = textSearchWord.getText().trim();
				if ("".equals(word) || null == word) {
					textSearchWordExplain.setText("");
					return;
				}
				String explain = dicCore.explainWord(word);
				if (null != explain)
					textSearchWordExplain.setText(explain);
				else
					textSearchWordExplain.setText(R.getR().getStrRes("msg_not_find"));
			}
		});
		searchItem.setControl(searchComposite);

		TabItem addItem = new TabItem(tabFolder, SWT.NONE);
		addItem.setText(R.getR().getStrRes("add_word"));
		Composite addComposite = new Composite(tabFolder, SWT.NONE);
		GridLayout layout1 = new GridLayout();
		layout1.numColumns=1;
		addComposite.setLayout(layout1);
		final Text textAddWord=new Text(addComposite, SWT.SINGLE|SWT.BORDER);
		final Text textAddWordExplain=new Text(addComposite, SWT.BORDER|SWT.WRAP);
		final Composite compositeAddButtons=new Composite(addComposite, SWT.NONE);
		GridData gd2 = new GridData();
		gd2.horizontalAlignment = GridData.FILL;
		gd2.grabExcessHorizontalSpace = true;
		textAddWord.setLayoutData(gd2);
		GridData gd3 = new GridData();
		gd3.horizontalAlignment = GridData.FILL;
		gd3.grabExcessHorizontalSpace = true;
		gd3.verticalAlignment = GridData.FILL;
		gd3.grabExcessVerticalSpace = true;
		textAddWordExplain.setLayoutData(gd3);
		GridData gd4=new GridData();
		gd4.horizontalAlignment=SWT.CENTER;
		compositeAddButtons.setLayoutData(gd4);
		RowLayout layout2=new RowLayout();
		compositeAddButtons.setLayout(layout2);
		final Button buttonAddOK=new Button(compositeAddButtons, SWT.PUSH);
		final Button buttonAddClear=new Button(compositeAddButtons, SWT.PUSH);
		buttonAddOK.setText(R.getR().getStrRes("ok"));
		buttonAddClear.setText(R.getR().getStrRes("clear"));
		buttonAddOK.pack();
		buttonAddClear.pack();
		textAddWord.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String word = textAddWord.getText().trim();
				if ("".equals(word) || null == word) {
					textAddWordExplain.setText("");
					return;
				}
				String explain = dicCore.explainWord(word);
				if (null != explain)
					textAddWordExplain.setText(explain);
				else 
					textAddWordExplain.setText("");
			}
		});
		buttonAddOK.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				String word = textAddWord.getText().trim();
				if ("".equals(word) || null == word) {
					textSearchWordExplain.setText("");
					return;
				}
				String explain = textAddWordExplain.getText().trim();
				if (!"".equals(explain)&&null != explain) {
					dicCore.addWord(word, explain);
					textAddWord.setText("");
					textAddWordExplain.setText("");
				}
			}
			
		});
		buttonAddClear.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				textAddWord.setText("");
				textAddWordExplain.setText("");
			}
			
		});
		addItem.setControl(addComposite);

		TabItem aboutItem = new TabItem(tabFolder, SWT.NONE);
		aboutItem.setText(R.getR().getStrRes("app_about"));
		Composite aboutComposite = new Composite(tabFolder, SWT.NONE);
		FillLayout layout3 = new FillLayout();
		layout3.type = SWT.VERTICAL;
		aboutComposite.setLayout(layout3);
		Link link = new Link(aboutComposite, SWT.NONE);
		link.setText(R.getR().getStrRes("msg_about_version") + "\n"
				+ R.getR().getStrRes("msg_about_author"));
		link.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				org.eclipse.swt.program.Program.launch(event.text);
			}
		});
		aboutItem.setControl(aboutComposite);

		tabFolder.pack();
		shell.setBounds(display.getClientArea().width / 2 - iWindowWidth / 2,
				display.getClientArea().height / 2 - iWindowHeight / 2,
				iWindowWidth, iWindowHeight);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}
